package org.lcogt.tools.fitsviewer.server;

import org.lcogt.tools.fitsviewer.client.GetRelatedService;
import org.lcogt.tools.fitsviewer.server.Generated.FITS;
import org.lcogt.tools.fitsviewer.server.Generated.FitsData;
import org.lcogt.tools.fitsviewer.server.Generated.FitsService;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.google.protobuf.RpcCallback;
import com.googlecode.protobuf.socketrpc.SocketRpcChannel;
import com.googlecode.protobuf.socketrpc.SocketRpcController;




public class GetRelatedServiceImpl extends RemoteServiceServlet implements GetRelatedService {
	String[] response;

	@Override
	public String[] getRelatedFits(String[] fields) {
		//The fields are the elements in the database for each fits file 
		SocketRpcChannel channel = new SocketRpcChannel("localhost", 8091);
	    SocketRpcController controller = channel.newRpcController();
		/* Build an empty request object */
		/* TODO: Work out how an RPC service in a .proto file can indicate
		 * no argument is needed.
		 */
		Generated.FitsService service = Generated.FitsService.newStub(channel);
		Generated.FITS.Builder builder = Generated.FITS.newBuilder();
		
		//Fills the request with all given fields
		for(int i = 1;i< fields.length+1;i++)
		{
			if(!fields[i-1].equals("None"))
			{
				
				if(i == 1)
				{
					builder.setFitsName(fields[i-1].trim());
				}	
				else if(i == 2)
				{
					builder.setTagID(fields[i-1]);
				}
				else if(i == 3)
				{
					builder.setProposalID(fields[i-1]);
				}
				else if(i == 4)
				{
					builder.setGroupID(fields[i-1]);
				}
				else if(i == 5)
				{
					builder.setObsID(fields[i-1]);
				}
				else if(i == 6)
				{	
					builder.setUserID(fields[i-1]);
				}
				else if(i == 7)
				{	
					builder.setTarget(fields[i-1]);
				}
				else if(i == 8)
				{
					builder.setExposure(fields[i-1]);
				}
				else if(i == 9)
				{	
					builder.setSite(fields[i-1]);
				}	
				else if(i == 10)
				{	
					builder.setTelescope(fields[i-1]);
				}
				else if(i == 11)
				{	
					builder.setInstrument(fields[i-1]);
				}
			}
			
		}
		Generated.FITS request = builder.build();
		//calls the getFitsFiles method from Generated.java
		service.getFitsFiles(controller, request, new RpcCallback<Generated.FitsData>()
		{

			@Override
			public void run(Generated.FitsData response) {
				//sets the response array to the correct size
				setResponse(response.getFitsFileCount());
				//loops through the response and fills the response array
				for(int i = 0; i < response.getFitsFileCount();i++)
				{
					if(response.getFitsFile(i).getStructurePathCount()>0)
					{
						fillResponse(i,response.getFitsFile(i).getStructurePath(0)+"");
					}
				}
				
			}
			
		});
		/* Report controller results. */

		System.out.println(controller.failed());
		System.out.println(controller.errorReason());
		System.out.println(controller.errorText());
		return response;
	}
	//sets the length of the response array
	void setResponse(int length)
	{
		response = new String[length];
	}
	//fills each index in the response array
	void fillResponse(int index,String fill)
	{
		response[index] = fill;
	}
}
